/**
 *
 */
package com.angel.io.descriptor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;

import com.angel.io.exceptions.InvalidFileAccessException;
import com.angel.io.separator.ColumnSeparator;

/**
 * @author William
 *
 */
public class ExportFileProcessorDescriptor extends FileProcessorDescriptor{

	private static final Logger LOGGER = Logger.getLogger(ExportFileProcessorDescriptor.class);

	public ExportFileProcessorDescriptor(String name, ColumnSeparator columnSeparator){
		super(name, columnSeparator);
	}

	/** Create a new instance object for {@link FileProcessorDescriptor} with a specific name, and
	 * a comma column separator.
	 *
	 * @param name for file processor descriptor.
	 * @see ColumnSeparator
	 */
	public ExportFileProcessorDescriptor(String name){
		super(name);
	}

	public OutputStream createOutputStream(String extension){
		String fileName = super.buildFileName() + extension;
		fileName = fileName.replaceAll("/", "-");
		LOGGER.info("Creating output stream for file name [" + fileName + "].");
		File file = new File(fileName);

		FileOutputStream fileOutputStream = null;
		try {
			file.createNewFile();
			fileOutputStream = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			throw new InvalidFileAccessException("File [" + file.getAbsolutePath() + "] not found because: " ,e );
		} catch (IOException e) {
			throw new InvalidFileAccessException("Could NOT create file with name [" + file.getName() + "] because: " ,e );
		}
		return fileOutputStream;
	}
}
